package org.dromara.germbank.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.germbank.domain.bo.BusOperationsAdvertisementBo;
import org.dromara.germbank.domain.vo.BusOperationsAdvertisementVo;
import org.dromara.germbank.service.AdvertisementService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 广告管理
 *
 * @author congxing
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ad")
public class BusOperationsAdvertisementController extends BaseController {

    private final AdvertisementService advertisementService;

    /**
     * 查询广告管理列表
     */
    @SaCheckPermission("germbank:advertisement:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperationsAdvertisementVo> list(BusOperationsAdvertisementBo bo, PageQuery pageQuery) {
        System.out.println("=== 请求到达微服务了！ ===");
        return advertisementService.queryPageList(bo,pageQuery);
    }

    /**
     * 新增广告
     */
    @SaCheckPermission("germbank:advertisement:add")
    @Log(title = "广告管理", businessType = BusinessType.INSERT)
//@RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody BusOperationsAdvertisementBo ad) {
        if (!advertisementService.checkAdNameUnique(ad)) {
            return R.fail("新增广告'" + ad.getName() + "'失败，广告名称已存在");
        }
        return toAjax(advertisementService.insertAd(ad));
    }

    /**
     * 修改保存广告
     */
    @SaCheckPermission("germbank:advertisement:edit")
    @Log(title = "广告数据", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody BusOperationsAdvertisementBo ad) {
        if (!advertisementService.checkAdNameUnique(ad)) {
            return R.fail("修改广告数据'" + ad.getName() + "'失败，广告已存在");
        }
        advertisementService.updateDictData(ad);
        return R.ok();
    }

    /**
     * 删除广告类型
     *
     * @param adCodes      * 删除广告类型code串
     */
    @SaCheckPermission("germbank:advertisement:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{adCodes}")
    public R<Void> remove(@PathVariable Long[] adCodes) {
        advertisementService.deleteAdDataByIds(Arrays.asList(adCodes));
        return R.ok();
    }


}
